package com.daiqee.controller;

import java.util.ArrayList;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.daiqee.controller.support.CommonsController;
import com.daiqee.enums.RequestType;
import com.daiqee.pojo.ProductionPrise;
import com.daiqee.pojo.SupplierArchives;
import com.daiqee.pojo.SupplierClassification;
import com.daiqee.pojo.SupplierLv;
import com.daiqee.service.impl.ProductionPriseService;
import com.daiqee.service.impl.SupplierClassificationService;
import com.daiqee.service.impl.SupplierLvService;
import com.daiqee.service.impl.SuppllierArchivesService;
import com.daiqee.util.WarpperStringUtils;
import com.daiqee.vo.BootstrapTablePageVO;
import com.daiqee.vo.LayuiVO;

/**
 *  产品报价
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.1
 * @author RichardTang 
 * @date: 2018年5月7日 下午5:55:03
 * @package com.daiqee.handler
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@Controller
@RequestMapping("procurement/production-prise")
public class ProductionPriseController extends CommonsController<ProductionPrise>{

	@Autowired
	private SupplierLvService supplierLvService;
	
	@Autowired
	private ProductionPriseService productionPriseService;
	
	@Autowired
	private SuppllierArchivesService supplierArchivesService;
	
	@Autowired
	private SupplierClassificationService supplierClassificationService;
	
	/**
	 * 打包产品报价模块所需要使用的数据
	 * 
	 * @return 视图和路径
	 */
	@RequestMapping(value="/pack-data",method=RequestMethod.GET)
	public ModelAndView packData() {
		
		//创建模型视图对象
		ModelAndView model = new ModelAndView();
		
		/***************************/
		/***查询所需要用到的数据****/
		/***************************/
		//查询供应商级别
		List<SupplierLv> supplierLv = supplierLvService.selectList(null);
		//查询供应商分类
		List<SupplierClassification> supplierClassifications = supplierClassificationService.selectList(null);
		//查询供应商档案
		List<SupplierArchives> supplierArchives = supplierArchivesService.selectList(null);
		//包装供应商档案中的值
		for (SupplierArchives itemSupplierArchives : supplierArchives) {
			itemSupplierArchives.setSupArchiCustomerClass(
					supplierClassifications.get(Integer.parseInt(itemSupplierArchives.getSupArchiCustomerClass())-1)
					.getSupClassName());
			itemSupplierArchives.setSupArchiCustomerLv(
					supplierLv.get(Integer.parseInt(itemSupplierArchives.getSupArchiCustomerLv())-1).getSupLvName());
		}
		/***************************/
		/***将数据添加到域对象中****/
		/***************************/
		model.addObject("supplierLv",supplierLv);	
		model.addObject("supplierClassification", supplierClassifications);
		model.addObject("supplierArchives",supplierArchives);
		
		model.setViewName("forward:/admin/enterpriseManagement/procurementManagement/productionPrise/production-prise.jsp");;
		return model;
	}
	
	/**
	 * 供应商id 查询对应产品
	 * 
	 * @param supId
	 * 			供应商id
	 * @param limit
	 * 			页码
	 * @param offset
	 * 			大小
	 * 
	 * @return 符合 bootstrap 解析格式的实体 
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public Object queryProductionPriseBySupId(
			@PathVariable(value="id") String supId,
			@RequestParam(value="limit",defaultValue = "0",required = true) Integer limit,
			@RequestParam(value="offset",defaultValue = "30",required = true) Integer offset) {
		
		EntityWrapper<ProductionPrise> ew = new EntityWrapper<ProductionPrise>();
		ew.eq("prod_supplier_id", supId);
		Page<ProductionPrise> page = new Page<ProductionPrise>(offset,limit);
		Page<ProductionPrise> pageResult = productionPriseService.selectPage(page,ew);
		BootstrapTablePageVO responseVO = new BootstrapTablePageVO(
		    pageResult.getPages(),pageResult.getTotal(),pageResult.getRecords());
		return restfulResponse.response(responseVO, RequestType.GET);
	}
	
	/**
	 * 产品id查询
	 * 
	 * @param prodId
	 * 			产品id
	 * @return restful风格 符合条件的 产品报价实体集合
	 */
	@RequestMapping(value="/prodId={id}",method=RequestMethod.GET)
	public Object queryProductById(@PathVariable(value="id") String prodId){
		LayuiVO<ProductionPrise> layuiJsonBox = null;
		List<String> prodIds = WarpperStringUtils.getIdListByCommaSplit(prodId);
		List<ProductionPrise> production = productionPriseService.selectBatchIds(prodIds);
		//设置供应商名称
		if(production != null) {
			List<Integer> supplierIds = new ArrayList<Integer>();
			//拿到所有对应的供应商id
			for(int i = 0;i<production.size();i++) {
				supplierIds.add(production.get(i).getProdSupplierId());
			}
			//查询到对应的供应商
			List<SupplierArchives> suppliers = supplierArchivesService.selectBatchIds(supplierIds);
			//从新封装值
			for(int i = 0;i<suppliers.size();i++) {
				production.get(i).setSupplierName(suppliers.get(i).getSupArchiEnterpriseName());
			}
		}
		if(production!=null) {
			layuiJsonBox = new LayuiVO<ProductionPrise>();
			layuiJsonBox.setCount(production.size());
			layuiJsonBox.setData(production);
		}
		return restfulResponse.response(layuiJsonBox,RequestType.GET);
	}
}
